package com.zyx.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyx.commonutils.R;
import com.zyx.eduservice.entity.EduCourse;
import com.zyx.eduservice.entity.EduTeacher;
import com.zyx.eduservice.entity.vo.CourseInfoVo;
import com.zyx.eduservice.entity.vo.CoursePublishVo;
import com.zyx.eduservice.entity.vo.CourseQuery;
import com.zyx.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zyx
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param id
     * @return
     */
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    /**
     * 课程最终发布
     * //修改课程状态
     */
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        //设置课程发布状态
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 删除课程
     *
     * @param courseId
     * @return
     */
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

    /////////////////////////////////////////////////////////////////

    /**
     * 返回所有课程
     *
     * @return
     */
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

//    /**
//     * 分页查询
//     */
//    @GetMapping("pageCourseList/{current}/{limit}")
//    public R pageCourse(@PathVariable long current,
//                        @PathVariable long limit) {
//
//        Page<EduCourse> page = new Page<>(current, limit);
//        eduCourseService.page(page, null);
//
//        long total = page.getTotal();
//        List<EduCourse> records = page.getRecords();
//
//        return R.ok().data("total", total).data("rows", records);
//    }

    /**
     *  条件查询分页方法
     */

    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        //创建page
        Page<EduCourse> pageCondition = new Page<>(page, limit);

        //QueryWrapper,构建
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询，动态sql
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        wrapper.orderByDesc("gmt_create");

        //调用方法，实现分页查询
        IPage<EduCourse> iPage = eduCourseService.page(pageCondition, wrapper);
        //获取总记录数
        long total = iPage.getTotal();
        //获取分页后的list集合
        List<EduCourse> records = iPage.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

}

